package com.wdf.project01.controller;

import com.wdf.project01.pojo.Result;
import com.wdf.project01.pojo.Student;
import com.wdf.project01.pojo.User;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.List;

@RestController
public class RequestController {
    //简单参数，使用Request对象解析
    @RequestMapping("/simpleParam")
    public String simpleParam(HttpServletRequest request){
        String name = request.getParameter("name");
        String ageStr = request.getParameter("age");
        int age = Integer.parseInt(ageStr);
        System.out.println(name + ":" + age);
        return "OK";
    }
    //直接使用方法参数接收
    @RequestMapping("/varParam")
    public String varParam(String name,Integer age){
        System.out.println(name + ":" + age);
        return "OK";
    }
    @RequestMapping("/requestParam")
    public Result requestParam(@RequestParam(name = "name") String userName, Integer age){
        System.out.println(userName + ":" + age);
        String result = userName + ":" + age;
        return Result.success(result);
    }
    //使用实体接收
    @RequestMapping("/requestPojo")
    public Result pojoParam(Student student){
        System.out.println(student);
        return Result.success(student);
    }
    //使用复杂的实体接收
    @RequestMapping("/complexPojo")
    public Result complexParam(User user){
        System.out.println(user);
        return Result.success(user);
    }
    @RequestMapping("/arrayParam")
    public Result arrayParam(String[] hobby){
        System.out.println(Arrays.toString(hobby));
        return Result.success(hobby);
    }
    @RequestMapping("/listParam")
    public Result listParam(@RequestParam List<String> hobby){
        System.out.println(hobby);
        return Result.success(hobby);
    }
    @RequestMapping("/dateParam")
    public Result dateParam(@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")LocalDateTime date){
        System.out.println(date);
        return Result.success(date);
    }
    @RequestMapping("/jsonParam")
    public Result jsonParam(@RequestBody User user){
        System.out.println(user);
        return Result.success(user);
    }
    @RequestMapping("/pathParam/{id}/{name}")
    public Result pathParam(@PathVariable Integer id,@PathVariable String name){
        System.out.println(name + ":" +id.toString());
        return Result.success("Ok");
    }
}
